﻿function openQQ() {
    window.open('tencent://message/?uin=<%=Config.CompanyQQ %>&Site=<%=Config.CompanyName %>&Menu=yes');
}

function onSearchBlur(obj) {
    var keyword = document.getElementById('txtSearch').value;
    if (keyword == '') {
        document.getElementById('txtSearch').value = '本站搜索';
        document.getElementById('txtSearch').style.color = '#ccc';
    }
}

function onSearchFocus(obj) {
    var keyword = document.getElementById('txtSearch').value;
    if (keyword == '本站搜索') {
        document.getElementById('txtSearch').value = '';
        document.getElementById('txtSearch').style.color = '#333';
    }
}

function topSearch() {
    var keyword = $('#txtSearch').val();
    if (!keyword) {
        alert('请输入搜索关键词!');
    }
    else {
        location = 'Search.aspx?Keyword=' + encodeURIComponent(keyword);
    }
    return false;
}

function fLoginKeyDown(evt) {
    var evt = evt || window.event; //兼容IE和FF
    if (evt.keyCode == 13) {
        return topLogin();
    }
}

function fSearchKeyDown() {
    var evt = evt || window.event; //兼容IE和FF
    if (evt.keyCode == 13) {
        return topSearch();
    }
}

function loginToQQ() {
    setCookie("urlBeforeLogin", location.href);
    location = 'GetQQInfo.aspx?OP=login';
}

function gotoPrevUrl() {
    var url = getCookie("urlBeforeLogin");
    if (url) location = url;
    else location = "";
}

function logout() {
    var r = -1;
    if (!confirm('您确定要退出此次登录?')) return;
    $.ajax({
        type: "GET",
        url: "GetQQInfo.aspx",
        data: "OP=logout&r=" + new Date().getUTCMilliseconds,
        dataType: "html",
        success: function (msg) {
            r = msg;
            if (r == 0) {
                delCookie("QQLoginId");
                delCookie("code");
                delCookie("state");
                if (location.href.match(/getqqinfo/i)) {
                    gotoPrevUrl();
                }
                else {
                    location = location;
                }
            } 
        }
    });
}

function setCookie(name, value) {
    document.cookie = name + "=" + escape(value);
}

function setCookieExpire(name, value, days) {
    var exp = new Date();
    exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg)) return unescape(arr[2]);
    else return null;
}

function delCookie(name) {//为了删除指定名称的cookie，可以将其过期时间设定为一个过去的时间
    var date = new Date();
    date.setTime(date.getTime() - 10000);
    document.cookie = name + "=; expires=" + date.toGMTString();
}
